﻿
Partial Class WebForm_AddVBook
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        'Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        'Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub
    Protected Sub rdoEditMode_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If Me.fv.FindRblControl("rdoEditMode").SelectedValue = "ReIssue" Then
            Me.fv.FindlblControl("lblOldComment").Visible = True
            Me.fv.FindTxtControl("txtOldComment").Visible = True
        Else
            Me.fv.FindlblControl("lblOldComment").Visible = False
            Me.fv.FindTxtControl("txtOldComment").Visible = False
        End If

    End Sub
    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv.ItemCommand
        If e.CommandName = "btnSave" Then
            If Me.fv.CurrentMode = FormViewMode.Insert Then
                Me.Visible = False
                RaiseEvent Saving(Me, New EventArgs)
            ElseIf Me.fv.FindRblControl("rdoEditMode").SelectedValue = "MyEdit" Then
                RaiseEvent Saving(Me, New EventArgs)
            Else
                RaiseEvent ReIssuing(Me, New EventArgs)
            End If
        ElseIf e.CommandName = "MyEdit" Or e.CommandName = "ReIssue" Then
            Me.fv.FindControl("plSave").Visible = True
        End If
    End Sub

End Class

Partial Class WebForm_AddVBook
    Public Event Saving(ByVal sender As Object, ByVal e As EventArgs)
    Public Event ReIssuing(ByVal sender As Object, ByVal e As EventArgs)

    Public Sub InitDataForInsert(ByVal volunteerID As String)
        Me.VolunteerID = volunteerID

        Me.DataAccessMode = My.DataAccessMode.Insert
        Me.Visible = True
        Me.fv.ChangeMode(FormViewMode.Insert)
        Me.fv.DataBind()

        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("select * from Volunteer v ")
        sql.AppendFormat("where VolunteerID = @VolunteerID")
        sql.AddWithValue("VolunteerID", volunteerID)

        Dim dt As Data.DataTable = dat.GetData(sql)

        Me.fv.FindlblControl("lblVolunteerNameText").Text = dt.Rows(0)("VolunteerName")
    End Sub

    Public Sub InitDataForEdit(ByVal id As String)
        Me.DataAccessMode = My.DataAccessMode.Update
        Me.Visible = True
        Me.fv.ChangeMode(FormViewMode.Edit)
        Me.BindData(id)
    End Sub

    Public Sub InitDataForRead(ByVal id As String)
        Me.DataAccessMode = My.DataAccessMode.Read
        Me.Visible = True
        Me.fv.ChangeMode(FormViewMode.ReadOnly)
        Me.BindData(id)
    End Sub

    Private Sub BindData(ByVal id As String)
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("select a.VolunteerName,b.* from VolunteerDistinctView a ")
        sql.AppendFormat(" inner join VBook b on a.VolunteerID=b.VolunteerID")
        sql.AppendFormat(" where b.ID = '{0}' ", id)

        Dim dt As Data.DataTable = dat.GetData(sql)

        Me.fv.DataSource = dt
        Me.fv.DataBind()

        Me.VolunteerID = dt.Rows(0)("VolunteerID")
    End Sub



    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Me.BindDDLSerialGroup()
        Me.BindCountyID()
        If Me.fv.DataItem IsNot Nothing Then
            If (DataBinder.Eval(Me.fv.DataItem, "SerialGroupID").ToString().Trim() <> "") Then
                TryCast(Me.fv.FindControl("ddlSerialGroup"), DropDownList).SelectedValue = _
        TryCast(Me.fv.FindControl("ddlSerialGroup"), DropDownList).Items.FindByValue(DataBinder.Eval(Me.fv.DataItem, "SerialGroupID").ToString()).Value
            End If
            If (DataBinder.Eval(Me.fv.DataItem, "CountyID").ToString().Trim() <> "") Then
                TryCast(Me.fv.FindControl("ddlCountyID"), DropDownList).SelectedValue = _
        TryCast(Me.fv.FindControl("ddlCountyID"), DropDownList).Items.FindByValue(DataBinder.Eval(Me.fv.DataItem, "CountyID").ToString()).Value
            End If
        End If

        If Me.DataAccessMode <> My.DataAccessMode.Read Then
            Me.fv.FindBtnControl("btnAdd").Attributes("onclick") = "this.disabled=true;" & Me.Page.ClientScript.GetPostBackEventReference(Me.fv.FindBtnControl("btnAdd"), "")
        End If

    End Sub

    Private Sub BindDDLSerialGroup()
        Dim ddlSerialGroup As DropDownList = TryCast(Me.fv.FindControl("ddlSerialGroup"), DropDownList)
        ddlSerialGroup.DataTextField = "SerialText"
        ddlSerialGroup.DataValueField = "Value"
        'ddlSerialGroup.DataSource = MyObj.GetCodeMappingData("VBook", "SerialGroupID", "Value+' '+Text as SerialText")
        ddlSerialGroup.DataSource = MyObj.GetCodeMappingData("VBook", "SerialGroupID", "Text as SerialText")
        ddlSerialGroup.DataBind()

        ddlSerialGroup.SelectedValue = "260"   '固定為環字第
        'ddlSerialGroup.Enabled = False
    End Sub

    Private Sub BindCountyID()
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("select * from RegionAlias ")
        sql.AppendFormat("where AliasGroup = '{0}' ", "VBook")
        sql.AppendFormat("order by AliasName ")

        Dim ddlCountyID As DropDownList = TryCast(Me.fv.FindControl("ddlCountyID"), DropDownList)
        ddlCountyID.DataTextField = "AliasName"
        ddlCountyID.DataValueField = "RegionID"
        ddlCountyID.DataSource = dat.GetData(sql)
        ddlCountyID.DataBind()

        ddlCountyID.SelectedValue = "TPC"
    End Sub

    Public ReadOnly Property PrimaryKey() As String
        Get
            If Me.fv.DataKey("ID") Is Nothing Then
                Return ""
            Else
                Return Me.fv.DataKey("ID")
            End If

        End Get
    End Property

    Public ReadOnly Property FormMode() As FormViewMode
        Get
            Return Me.fv.CurrentMode
        End Get
    End Property

    Public Property VolunteerID() As String
        Get
            Return ViewState("VolunteerID")
        End Get
        Set(ByVal value As String)
            ViewState("VolunteerID") = value
        End Set
    End Property

    Public ReadOnly Property CountyID() As String
        Get
            Return TryCast(Me.fv.FindControl("ddlCountyID"), DropDownList).SelectedValue
        End Get
    End Property

    Public ReadOnly Property SerialGroupID() As String
        Get
            Return TryCast(Me.fv.FindControl("ddlSerialGroup"), DropDownList).SelectedValue
        End Get
    End Property

    Public ReadOnly Property SerialNumber() As String
        Get
            Return TryCast(Me.fv.FindControl("txtSerialNumber"), TextBox).Text
        End Get
    End Property

    Public ReadOnly Property StatePaperID() As String
        Get
            Return Me.fv.FindTxtControl("txtStatePaperID").Text
        End Get
    End Property

    Public ReadOnly Property IssueDate() As DateTime
        Get
            Return DateTime.Parse(TryCast(Me.fv.FindControl("txtIssueDate"), WebForm_CommonControl_CalendarTextBox).Text)
        End Get
    End Property

    Public ReadOnly Property Comment() As String
        Get
            If Me.fv.FindTxtControl("txtComment") Is Nothing Then
                Return ""
            Else
                Return Me.fv.FindTxtControl("txtComment").Text
            End If

        End Get
    End Property
    Public ReadOnly Property OldComment() As String
        Get
            If Me.fv.FindTxtControl("txtOldComment") Is Nothing Then
                Return ""
            Else
                Return Me.fv.FindTxtControl("txtOldComment").Text
            End If

        End Get
    End Property
    Public Property DataAccessMode() As My.DataAccessMode
        Get
            If ViewState("DataAccessMode") Is Nothing Then
                Return My.DataAccessMode.Insert
            End If

            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As My.DataAccessMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property
End Class